/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.DAOs;


import com.ControlEscolar.Beans.Subject;
import java.util.ArrayList;


/**
 *
 * @author Listico
 */
public class SubjectDAO extends AbstractDAO <Subject> {

    @Override
    public void add(Subject object) throws DAOException {
        String sql = "INSERT INTO subjects (subjectKey, subjectName,subjectCredits,subjectTotalHr)values(\""

                +object.getSubjectKey()+ "\",\""
                + object.getSubjectName()+"\","
                + object.getSubjectCredits()+","
                + object.getSubjectTotalHr()+");";

        try {
            connection.executeUpdate(sql);
        } catch (Exception ex) {
            throw new DAOException();
        }

    }

    @Override
    public int delete(String subjectKey) throws DAOException {
      String sql = "DELETE FROM subjects where subjectKey= \""+subjectKey+"\"";
        try {
            connection.executeUpdate(sql);
        } catch (Exception ex) {
            throw new DAOException();
        }
    return 0;
    }

    @Override
    public int update(Subject subject) throws DAOException {

        String sql="UPDATE subjects SET subjectName=\""+subject.getSubjectName()+"\", "+
                "subjectCredits= "+subject.getSubjectCredits()+", "+
                "subjectTotalHr="+subject.getSubjectTotalHr()+
                " where subjectKey = \""+subject.getSubjectKey()+"\"";
        try {
            connection.executeUpdate(sql);
        } catch (Exception ex) {
            throw new DAOException();
        }


        return 0;
    }

    @Override
    public ArrayList<Subject> query() throws DAOException {
        try {
            String sql = "SELECT * FROM subjects";
            return connection.executeQuery(sql, Subject.class);

        } catch (Exception ex) {
            throw new DAOException();
        }
    }

     public Subject queryByKey(String subjectKey) throws DAOException {
        try {
            String sql = "SELECT * FROM subjects where subjectKey = \""+subjectKey+"\"";
            ArrayList<Subject> subjectQuery=connection.executeQuery(sql, Subject.class);
            return getSpecificQuery(subjectQuery);

        } catch (Exception ex) {
            throw new DAOException();
        }
    }
    
   


}

